<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Admin_Form_Member_Create extends RedCross_Form
{
    public function init()
    {
        $this->addElement('text', 'first_name', array(
            'filters'    => array('StringTrim'),
            'validators'    =>  array(
                array('NotEmpty', true),
                array('Alpha', true, true),
                array('StringLength', true, array(2,32)),
            ),
            'required'  => true,
            'label'     => 'Fornavn'
        ));

        $this->addElement('text', 'last_name', array(
            'filters'    => array('StringTrim'),
            'validators' =>  array(
                array('NotEmpty', true),
                array('Alpha', true, true),
                array('StringLength', true, array(2,32)),
            ),
            'required'  => true,
            'label'     => 'Etternavn'
        ));

        $this->addElement('text', 'address', array(
            'filters'    => array('StringTrim'),
            'validators' =>  array(
                array('Alnum', true, true),
                array('StringLength', true, array(4,32)),
            ),
            'label'     => 'Adresse'
        ));

        $this->addElement('select', 'postal_code', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('NotEmpty', true),
                array('Digits', true),
            ),
            'label' => 'Postnummer'
        ));

        $this->getElement('postal_code')->addMultiOption(null, '-- Velg postnummer --');
        
        $this->addElement('text', 'email', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('StringLength', true, array(6, 60)),
                array('EmailAddress', true),
                array('UniqueEmail', true),
            ),
            'required'  => true,
            'label'     => 'Epost'
        ));

        $this->addElement('text', 'phone', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('Digits'),
                array('StringLength', true, array(8, 8)),
            ),
            'required'  => true,
            'label'     => 'Telefon'
        ));

        $this->addElement('select', 'phonetype', array(
            'required'  => true,
            'label'     => 'Telefontype'
        ));

        $this->addElement('text', 'redcross_id', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                array('Digits'),
            ),
            'required'  => false,
            'label'     => 'Medlemsnummer'
        ));

        $this->addElement('submit', 'submit', array(
            'required' => true,
            'ignore'   => true,
            'label'    => 'Opprett',
        ));

        $this->fillPostalCodes();
        $this->fillPhoneTypes();
    }

    private function fillPostalCodes()
    {
        $postalService = RedCross_Factory::getService('Core_Service_Postal');
        $postalCodes = $postalService->fetchAll();
        $postalElement = $this->getElement('postal_code');
        foreach($postalCodes as $postal) {
            $postalElement->addMultiOption($postal['id'], $this->getPostalCode($postal['id']) . ' - ' . $postal['area']);
        }
    }

    private function getPostalCode($postal_id)
    {
        $length = strlen($postal_id);
        switch($length) {
            case 1:
                return '000' . $postal_id;
                break;
            case 2:
                return '00' . $postal_id;
                break;
            case 3:
                return '0' . $postal_id;
                break;
        }
        return $postal_id;
    }

    private function fillPhoneTypes()
    {
        $phonetypeService = RedCross_Factory::getService('Core_Service_Phonetype');
        $phonetypes = $phonetypeService->fetchAll();

        foreach($phonetypes as $phonetype) {
            $this->getElement('phonetype')->addMultiOption($phonetype->id, $phonetype->name);
        }
    }

}
